<?php
	/**
 	 * ViewSource class
 	 */
	class ViewSourceController extends Controller
	{
		/**
	 	 * page title
	 	 */
		public $page_title = LANG_VIEW_SOURCE;
		
		/**
	 	 * page layout
	 	 */
		public $layout = 'main';
		
		/**
	 	 * necessary helpers
	 	 */
		public $helpers = array ("html", "htmlx", "toolbars");
		
		/**
	 	 * necessary javascript files
	 	 */
		public $javascript = array("TreeMenu");
		
		/**
	 	 * necessary components
	 	 */
		public $components = array ("TreeMenu");
		
		/**
		 * controller name
		 *
		 * @var string
		 */
		public $name = "ViewSource";
		
		/**
		 * Saves the file (editor contents) and backups the old file version in the ./backup folder
		 *
		 */
		function SaveFile()
		{
			//creates the file with the specified name and backups the old one
		    copy($this->request->post[path_to_file], $_SERVER['DOCUMENT_ROOT'].'/app/spanel_backup/'.pathinfo($this->request->post[path_to_file], PATHINFO_BASENAME).'_'.date('Y_m_d_H_i_s').'.bak');
			$fp = fopen($this->request->post[path_to_file], 'w+');

			//writes to the file
			$content = iconv('UTF-8', 'windows-1251', $this->request->post[content]);
			fwrite($fp, $content);
			
			//closes the file
			fclose($fp);				
						
			exit();
		}
		
		/**
	 	 * displays object source
	 	 */
		function Index($type = '', $name = null, $subname = null)
		{
			if (isset($this->request->get[path_to_file]))
			{
				$this->data[path_to_file] = $this->request->get[path_to_file];
				$this->data[file] = file_get_contents($this->request->get[path_to_file]);
				$this->data[file_extension] = pathinfo($this->data[path_to_file], PATHINFO_EXTENSION);
			}
			else
			{
				switch ($type)
				{
					case "controller":
					{
						$this->data[path_to_file] = "./app/controllers/".$name.'.php';
						$this->data[file] = file_get_contents($this->data[path_to_file]);
						break;
					}
					case "model":
					{
						$this->data[path_to_file] = "./app/models/".$name.'.php';
						$this->data[file] = file_get_contents($this->data[path_to_file]);
						break;
					}
					case "view":
					{
						$this->data[path_to_file] = "./app/views/".$name.'/'.$subname;
						$this->data[file] = file_get_contents($this->data[path_to_file]);
						break;
					}
				}
				
				$this->data[file_extension] = 'SpherusPHP';
			}
			
			$this->data[type] = $type;
			$this->data[name] = $name;
		}
	}
?>